/*
 * OLED.c
 * 
 *  Created on: Nov 16, 2024
 *      Author: 202440291020
 */
#include "OLED.h"
#include "OLED_FONT.h"

/*=========================内部函数============================*/
void _writeCMD(uint8_t cmd)
{
	HAL_I2C_Mem_Write(&hi2c1, OLED_DEV_ADDRESS, OLED_CMD_ADDRESS, I2C_MEMADD_SIZE_8BIT, &cmd, 1, 0x100);
}
void _writeDATA(uint8_t data)
{
	HAL_I2C_Mem_Write(&hi2c1, OLED_DEV_ADDRESS, OLED_DATA_ADDRESS, I2C_MEMADD_SIZE_8BIT, &data, 1, 0x100);
}
// 设置光标位置
void _setPosition(uint8_t page,uint8_t x)
{
	_writeCMD(0x00 | (x & 0x0F));
	_writeCMD(0x10 | ((x & 0xF0) >> 4));
	_writeCMD(0xB0 | page);
}
// 取数字位
void _getNumPost(unsigned int num, uint8_t length, uint8_t* array)
{
	for(uint8_t i = 0; i < length; i++)
	{
		array[length - i - 1] = num % 10;
		num = num / 10;
	}
}

/*=========================外部函数============================*/
// 初始化
void OLED_Init(void)
{
	uint8_t CMD_Data[]={
		0xAE, 0xD5, 0x80, 0xA8, 0x3F, 0xD3, 0x00, 0x40,0xA1, 0xC8, 0xDA,
		0x12, 0x81, 0xCF, 0xD9, 0xF1, 0xDB, 0x40, 0xA4, 0xA6,0x8D, 0x14,
		0xAF};
	for(uint8_t i = 0; i < 23; i++)
	{
		_writeCMD(CMD_Data[i]);
	}
	OLED_Clear();
}
// 清屏
void OLED_Clear(void)
{
	for(uint8_t i = 0; i < 8; i++)
	{
		for(uint8_t j = 0; j < 128; j++)
		{
			_setPosition(i, j);
			_writeDATA(0x00);
		}
	}
}

// 显示字符
void OLED_ShowChar8X16( uint8_t page,uint8_t x, char charIndex)
{
	uint8_t index = charIndex - ' ';
	_setPosition(page, x);
	for(uint8_t i = 0; i < 8; i++)
	{
		_writeDATA(char8X16[index * 2][i]);
	}
	_setPosition(page + 1, x);
	for(uint8_t i = 0; i < 8; i++)
	{
		_writeDATA(char8X16[index * 2 + 1][i]);
	}
}
void OLED_ShowChar6X8(uint8_t page,uint8_t x, char charIndex)
{
	uint8_t index = charIndex - ' ';
	_setPosition(page, x);
	for(uint8_t i = 0; i < 6; i++)
	{
		_writeDATA(char6X8[index][i]);
	}
}
// 显示汉字
void OLED_ShowChinese16X16(uint8_t page,uint8_t x, uint8_t chineseIndex,uint8_t font[4][16])
{
	_setPosition(page, x);
	for(uint8_t i = 0; i < 16; i++)
	{
		_writeDATA(font[chineseIndex * 2][i]);
	}
	_setPosition(page + 1, x);
	for(uint8_t i = 0; i < 16; i++)
	{
		_writeDATA(font[chineseIndex * 2 + 1][i]);
	}
}
//显示长汉字
void OLED_ShowLongChinese16X16(uint8_t page,uint8_t x, uint8_t Length,uint8_t font[4][16])
{
	for(uint8_t chineseIndex = 0;chineseIndex<Length;chineseIndex++)
	{
		_setPosition(page, x+chineseIndex*16);
		for(uint8_t i = 0; i < 16; i++)
		{
			_writeDATA(font[chineseIndex * 2][i]);
		}
		_setPosition(page + 1, x+chineseIndex*16);
		for(uint8_t i = 0; i < 16; i++)
		{
			_writeDATA(font[chineseIndex * 2 + 1][i]);
		}
	}
}

// 显示字符
void OLED_ShowString8X16(uint8_t page,uint8_t x, char* string)
{
	for(uint8_t index = 0; string[index] != '\0'; index++)
	{
		OLED_ShowChar8X16(x + index * 8, page, string[index]);
	}
}
void OLED_ShowString6X8(uint8_t page,uint8_t x, char* string)
{
	for(uint8_t index = 0; string[index] != '\0'; index++)
	{
		OLED_ShowChar6X8(x + index * 6, page, string[index]);
	}
}
// 显示图像
void OLED_ShowIMG(uint8_t page,uint8_t x, uint8_t imgWeight, uint8_t imgHeight)
{
	for(uint8_t i = 0; i < imgHeight; i++)
	{
		_setPosition(x, page + i);
		for(uint8_t j = 0; j < imgWeight; j++)
		{
			_writeDATA(image[i * imgWeight + j]);
		}
	}
}
// 显示数字
void OLED_ShowNum8X16(uint8_t page,uint8_t x, unsigned int num, uint8_t length)
{
	uint8_t numArray[length];
	_getNumPost(num, length, &numArray[0]);
	for(uint8_t i = 0; i < length; i++)
	{
		OLED_ShowChar8X16(page,x + i * 8, numArray[i] + '0');
	}
}
void OLED_ShowNum6X8(uint8_t page,uint8_t x, unsigned int num, uint8_t length)
{
	uint8_t numArray[length];
	_getNumPost(num, length, &numArray[0]);
	for(uint8_t i = 0; i < length; i++)
	{
		OLED_ShowChar6X8(page,x + i * 8, numArray[i] + '0');
	}
}
// 显示符号数字
void OLED_ShowSignedNum8X16(uint8_t page,uint8_t x, int num, uint8_t length)
{
	if(num >= 0)
	{
		uint8_t numArray[length];
		_getNumPost(num, length, &numArray[0]);
		for(uint8_t i = 0; i < length; i++)
		{
			OLED_ShowChar8X16(page,x + i * 8, numArray[i] + '0');
		}
	}
	if(num < 0)
	{
		num = -num;
		uint8_t numArray[length-1];
		_getNumPost(num, length-1, &numArray[0]);
		OLED_ShowChar8X16(page,x,'-');
		for(uint8_t i = 0; i < length-1; i++)
		{
			OLED_ShowChar8X16(page,x +8+ i * 8, numArray[i] + '0');
		}
	}
}
